<?php
namespace app\common\controller;

/**
 * 社区巡店类
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/06
 * Time: 15:09
 */
class Cp extends Patrol {

    /**
     * 计算社区巡店开始时间和结束时间
     * @return mixed
     */
    public function count_cp_time(){
        $maps = [];
        $maps[] = ['key','=','cp_time'];
        $conf_info = model('conf')->get_list($maps);
        foreach($conf_info as $k => $v){
            $conf_info_new[$v['key']] = $v['value'];
        }
        $cp_time = json_decode($conf_info_new['cp_time'],true);

        $now_time = date('H:i');
        foreach($cp_time as $k => $v){
            if($now_time >= $v['start_time'] && $now_time < $v['end_time']){
                $start_time = $v['start_time'];
                $end_time = $v['end_time'];
                $code = $v['code'];
                break;
            }else{
                $start_time = '00:00';
                $end_time = '00:00';
                $code = 'other';
            }
        }

        $data['start_time'] = $start_time;
        $data['end_time'] = $end_time;
        $data['code'] = $code;
        return $data;
    }

    /**
     * 获取社区巡店分部信息
     * @param $branchid
     * @return mixed
     */
    public function get_cp_branch_info($branchid){
        //计算社区巡店开始时间和结束时间
        $cp_time = $this->count_cp_time();
        $now_date = date('Y-m-d');

        $maps = [];
        $maps[] = ['branchid','=',$branchid];
        $maps[] = ['time_code','=',$cp_time['code']];
        $maps[] = ['now_date','=',$now_date];
        $cb_detail = model('cp_branch')->get_detail($maps,'id,branchid,status');
        if(empty($cb_detail)){
            return 0;
        }
        $cb_detail['status_zw'] = $cb_detail->status_name;

        $data['id'] = $cb_detail['id'];
        $data['branchid'] = $cb_detail['branchid'];
        $data['status'] = $cb_detail['status'];
        $data['status_zw'] = $cb_detail['status_zw'];
        return $data;
    }

    /**
     * 获取社区巡店楼层信息
     * @param $cp_branchid
     * @param $branchid
     * @param $floorid
     * @return array
     */
    public function get_cp_floor_info($cp_branchid,$branchid,$floorid){
        $data = [];

        $maps = [];
        $maps[] = ['cp_branchid','=',$cp_branchid];
        $maps[] = ['branchid','=',$branchid];
        $maps[] = ['floorid','=',$floorid];
        $maps[] = ['status','=',1];
        $cf_detail = model('cp_floor')->get_detail($maps,'id,start_time,end_time,status','id desc');
        if(!empty($cf_detail)){
            $data['end_time'] = $cf_detail['end_time'];
            $data['status'] = $cf_detail['status'];
            $data['status_zw'] = $cf_detail->status_name;
        }else{
            $maps = [];
            $maps[] = ['cp_branchid','=',$cp_branchid];
            $maps[] = ['branchid','=',$branchid];
            $maps[] = ['floorid','=',$floorid];
            $cf_detail = model('cp_floor')->get_detail($maps,'id,start_time,end_time,status');
            if(empty($cf_detail)){
                $data['end_time'] = '0000-00-00 00:00:00';
                $data['status'] = 0;
                $data['status_zw'] = '未完成';
            }
            if($cf_detail['status'] == 2){
                $data['end_time'] = $cf_detail['start_time'];
                $data['status'] = $cf_detail['status'];
                $data['status_zw'] = $cf_detail->status_name;
            }
        }
        return $data;
    }

    /**
     * 处理社区巡店工位“是否加班”显示
     * @param $cp_branchid
     * @return mixed
     */
    public function deal_cp_is_show_work($cp_branchid){
        $maps = [];
        $maps[] = ['id','=',$cp_branchid];
        $cb_detail = model('cp_branch')->get_detail($maps,'id,time_code,now_date');
        if(!empty($cb_detail) && $cb_detail['time_code'] == 'C'){
            $is_show_evening_work = 1;
        }else{
            $is_show_evening_work = 0;
        }

        $now_date = date('Y-m-d',strtotime('+1 day',strtotime($cb_detail['now_date'])));
        $h = new Holiday();
        $rh = $h->get_date_holiday($now_date);
        if($rh['holiday'] == 1){
            $is_show_holiday_work = 1;
        }else{
            $is_show_holiday_work = 0;
        }

        $data['is_show_evening_work'] = $is_show_evening_work;
        $data['is_show_holiday_work'] = $is_show_holiday_work;
        return $data;
    }
}